package com.kust.commomUtils;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 马强
 * @Description
 * @create 2022-06-14 17:12
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class Result {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data = new HashMap<String,Object>();


    private Result(){}

    // 返回数据
    private static  Result build(Boolean success) {
        Result result = new Result();
        result.setSuccess(success);
        return result;
    }

    private static  Result build(Boolean success,ResultCodeEnum resultCodeEnum) {
        Result result = build(success);
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    //成功静态方法
    public static Result ok(){
        Result result = new Result();
        return build(true,ResultCodeEnum.SUCCESS);
    }

    //失败静态方法
    public static Result error(){
        Result result = new Result();
        return build(false,ResultCodeEnum.FAIL);
    }


    //链式编程
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
                this.setCode(code);
                return this;
            }

    public Result data(String key, Object value){
                this.data.put(key, value);
                return this;
            }

    public Result data(Map<String, Object> map){
                this.setData(map);
                return this;
            }
}
